import java.awt.Component;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public final class Sprite extends Renderer2D {
	public int[] pixels;
	public int width;
	public int height;
	public int offsetX;
	public int offsetY;
	public int trimWidth;
	public int trimHeight;

	public Sprite(int i, int i_0_) {
		pixels = new int[i * i_0_];
		width = trimWidth = i;
		height = trimHeight = i_0_;
		offsetX = offsetY = 0;
	}

	public Sprite(byte[] data, Component component) {
		try {
			Image image = Toolkit.getDefaultToolkit().createImage(data);
			MediaTracker mediatracker = new MediaTracker(component);
			mediatracker.addImage(image, 0);
			mediatracker.waitForAll();
			width = image.getWidth(component);
			height = image.getHeight(component);
			trimWidth = width;
			trimHeight = height;
			offsetX = 0;
			offsetY = 0;
			pixels = new int[width * height];
			PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, width, height,
				pixels, 0, width);
			pixelgrabber.grabPixels();
		} catch (Exception exception) {
			System.out.println("Error converting jpg");
		}
	}

	public Sprite(Archive archive, String name, int requestIndex) {
		Packet data = new Packet(archive.method154(name + ".dat", null));
		Packet cacheIndex = new Packet(archive.method154("index.dat", null));
		cacheIndex.offset = data.getUShort();
		trimWidth = cacheIndex.getUShort();
		trimHeight = cacheIndex.getUShort();
		int src = cacheIndex.getUByte();
		int[] dest = new int[src];
		for (int i_3_ = 0; i_3_ < src - 1; i_3_++) {
			dest[i_3_ + 1] = cacheIndex.get24BitInt1();
			if (dest[i_3_ + 1] == 0) {
				dest[i_3_ + 1] = 1;
			}
		}
		for (int i_4_ = 0; i_4_ < requestIndex; i_4_++) {
			cacheIndex.offset += 2;
			data.offset += (cacheIndex.getUShort() * cacheIndex.getUShort());
			cacheIndex.offset++;
		}
		offsetX = cacheIndex.getUByte();
		offsetY = cacheIndex.getUByte();
		width = cacheIndex.getUShort();
		height = cacheIndex.getUShort();
		int unknown_boolean = cacheIndex.getUByte();
		int pixelCount = width * height;
		pixels = new int[pixelCount];
		if (unknown_boolean == 0) {
			for (int pixelLoop = 0; pixelLoop < pixelCount; pixelLoop++) {
				pixels[pixelLoop] = dest[data.getUByte()];
			}
		} else if (unknown_boolean == 1) {
			for (int i_8_ = 0; i_8_ < width; i_8_++) {
				for (int i_9_ = 0; i_9_ < height; i_9_++) {
					pixels[i_8_ + i_9_ * width] = dest[data.getUByte()];
				}
			}
		}
	}

	public void method467() {
		Renderer2D.method455(width, height, pixels);
	}

	public void method468(int i, int i_10_, int i_11_) {
		for (int i_13_ = 0; i_13_ < pixels.length; i_13_++) {
			int i_14_ = pixels[i_13_];
			if (i_14_ != 0) {
				int i_15_ = i_14_ >> 16 & 0xff;
				i_15_ += i_11_;
				if (i_15_ < 1) {
					i_15_ = 1;
				} else if (i_15_ > 255) {
					i_15_ = 255;
				}
				int i_16_ = i_14_ >> 8 & 0xff;
				i_16_ += i_10_;
				if (i_16_ < 1) {
					i_16_ = 1;
				} else if (i_16_ > 255) {
					i_16_ = 255;
				}
				int i_17_ = i_14_ & 0xff;
				i_17_ += i;
				if (i_17_ < 1) {
					i_17_ = 1;
				} else if (i_17_ > 255) {
					i_17_ = 255;
				}
				pixels[i_13_] = (i_15_ << 16) + (i_16_ << 8) + i_17_;
			}
		}
	}

	public void method469(int i) {
		int[] is = new int[trimWidth * trimHeight];
		for (int i_18_ = 0; i_18_ < height; i_18_++) {
			for (int i_19_ = 0; i_19_ < width; i_19_++) {
				is[(i_18_ + offsetY) * trimWidth + (i_19_ + offsetX)] = pixels[i_18_
					* width + i_19_];
			}
		}
		pixels = is;
		width = trimWidth;
		height = trimHeight;
		offsetX = 0;
		offsetY = 0;
		if (i != 1790) {
			/* empty */
		}
	}

	public void method470(int i, int i_21_) {
		i_21_ += offsetX;
		i += offsetY;
		int i_22_ = i_21_ + i * Renderer2D.width;
		int i_23_ = 0;
		int i_24_ = height;
		int i_25_ = width;
		int i_26_ = Renderer2D.width - i_25_;
		int i_27_ = 0;
		if (i < Renderer2D.topY) {
			int i_28_ = Renderer2D.topY - i;
			i_24_ -= i_28_;
			i = Renderer2D.topY;
			i_23_ += i_28_ * i_25_;
			i_22_ += i_28_ * Renderer2D.width;
		}
		if (i + i_24_ > Renderer2D.bottomY) {
			i_24_ -= i + i_24_ - Renderer2D.bottomY;
		}
		if (i_21_ < Renderer2D.topX) {
			int i_29_ = Renderer2D.topX - i_21_;
			i_25_ -= i_29_;
			i_21_ = Renderer2D.topX;
			i_23_ += i_29_;
			i_22_ += i_29_;
			i_27_ += i_29_;
			i_26_ += i_29_;
		}
		if (i_21_ + i_25_ > Renderer2D.bottomX) {
			int i_30_ = i_21_ + i_25_ - Renderer2D.bottomX;
			i_25_ -= i_30_;
			i_27_ += i_30_;
			i_26_ += i_30_;
		}
		if (i_25_ > 0 && i_24_ > 0) {
			method471(i_25_, i_26_, i_24_, pixels, i_23_, i_27_, i_22_, Renderer2D.pixels);
		}
	}

	private void method471(int i, int i_31_, int i_32_, int[] is, int i_33_, int i_34_, int i_35_,
		int[] is_37_) {
		int i_38_ = -(i >> 2);
		i = -(i & 0x3);
		for (int i_39_ = -i_32_; i_39_ < 0; i_39_++) {
			for (int i_40_ = i_38_; i_40_ < 0; i_40_++) {
				is_37_[i_35_++] = is[i_33_++];
				is_37_[i_35_++] = is[i_33_++];
				is_37_[i_35_++] = is[i_33_++];
				is_37_[i_35_++] = is[i_33_++];
			}
			for (int i_41_ = i; i_41_ < 0; i_41_++) {
				is_37_[i_35_++] = is[i_33_++];
			}
			i_35_ += i_31_;
			i_33_ += i_34_;
		}
	}

	public void drawSprite(int x, int y) {
		x += offsetX;
		y += offsetY;
		int destOffset = x + y * Renderer2D.width;
		int srcOffset = 0;
		int height = this.height;
		int width = this.width;
		int destStep = Renderer2D.width - width;
		int srcStep = 0;
		if (y < Renderer2D.topY) {
			int trimHeight = Renderer2D.topY - y;
			height -= trimHeight;
			y = Renderer2D.topY;
			srcOffset += trimHeight * width;
			destOffset += trimHeight * Renderer2D.width;
		}
		if (y + height > Renderer2D.bottomY) {
			height -= y + height - Renderer2D.bottomY;
		}
		if (x < Renderer2D.topX) {
			int trimLeft = Renderer2D.topX - x;
			width -= trimLeft;
			x = Renderer2D.topX;
			srcOffset += trimLeft;
			destOffset += trimLeft;
			srcStep += trimLeft;
			destStep += trimLeft;
		}
		if (x + width > Renderer2D.bottomX) {
			int trimRight = x + width - Renderer2D.bottomX;
			width -= trimRight;
			srcStep += trimRight;
			destStep += trimRight;
		}
		if (width > 0 && height > 0) {
			setPixels(Renderer2D.pixels, pixels, 0, srcOffset, destOffset, width, height,
				destStep,
				srcStep);
		}
	}

	private void setPixels(int[] destPixels, int[] srcPixels, int color, int srcOffset, int destOffset, int width,
		int height, int destStep, int srcStep) {
		int destWidth = -(width >> 2);
		width = -(width & 0x3);
		for (int i_61_ = -height; i_61_ < 0; i_61_++) {
			for (int i_62_ = destWidth; i_62_ < 0; i_62_++) {
				color = srcPixels[srcOffset++];
				if (color != 0) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if (color != 0) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if (color != 0) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if (color != 0) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
			}
			for (int i_63_ = width; i_63_ < 0; i_63_++) {
				color = srcPixels[srcOffset++];
				if (color != 0) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
			}
			destOffset += destStep;
			srcOffset += srcStep;
		}
	}

	public void method474(int i, int i_64_, int i_65_, int i_66_) {
		i_64_ += offsetX;
		i_65_ += offsetY;
		int i_67_ = i_64_ + i_65_ * Renderer2D.width;
		int i_68_ = 0;
		if (i == 0) {
			int i_69_ = height;
			int i_70_ = width;
			int i_71_ = Renderer2D.width - i_70_;
			int i_72_ = 0;
			if (i_65_ < Renderer2D.topY) {
				int i_73_ = Renderer2D.topY - i_65_;
				i_69_ -= i_73_;
				i_65_ = Renderer2D.topY;
				i_68_ += i_73_ * i_70_;
				i_67_ += i_73_ * Renderer2D.width;
			}
			if (i_65_ + i_69_ > Renderer2D.bottomY) {
				i_69_ -= i_65_ + i_69_ - Renderer2D.bottomY;
			}
			if (i_64_ < Renderer2D.topX) {
				int i_74_ = Renderer2D.topX - i_64_;
				i_70_ -= i_74_;
				i_64_ = Renderer2D.topX;
				i_68_ += i_74_;
				i_67_ += i_74_;
				i_72_ += i_74_;
				i_71_ += i_74_;
			}
			if (i_64_ + i_70_ > Renderer2D.bottomX) {
				int i_75_ = i_64_ + i_70_ - Renderer2D.bottomX;
				i_70_ -= i_75_;
				i_72_ += i_75_;
				i_71_ += i_75_;
			}
			if (i_70_ > 0 && i_69_ > 0) {
				method475(i_70_, i_72_, 0, i_71_, i_68_, i_66_, i_67_, i_69_, Renderer2D.pixels,
					pixels);
			}
		}
	}

	private void method475(int i, int i_76_, int i_77_, int i_78_, int i_79_, int i_81_, int i_82_,
		int i_83_, int[] is, int[] is_84_) {
		int i_85_ = 256 - i_81_;
		for (int i_86_ = -i_83_; i_86_ < 0; i_86_++) {
			for (int i_87_ = -i; i_87_ < 0; i_87_++) {
				i_77_ = is_84_[i_79_++];
				if (i_77_ != 0) {
					int i_88_ = is[i_82_];
					is[i_82_++] = ((((i_77_ & 0xff00ff) * i_81_ + (i_88_ & 0xff00ff) * i_85_) & ~0xff00ff) + (((i_77_ & 0xff00)
						* i_81_ + (i_88_ & 0xff00) * i_85_) & 0xff0000)) >> 8;
				} else {
					i_82_++;
				}
			}
			i_82_ += i_78_;
			i_79_ += i_76_;
		}
	}

	public void method476(int i, int i_89_, int i_90_, int i_91_, int i_92_, int[] is, int i_93_,
		int i_94_, int i_95_, int[] is_96_, int i_97_) {
		i_89_ = 36 / i_89_;
		try {
			int i_98_ = -i_92_ / 2;
			int i_99_ = -i_90_ / 2;
			int i_100_ = (int) (Math.sin(i_94_ / 326.11) * 65536.0);
			int i_101_ = (int) (Math.cos(i_94_ / 326.11) * 65536.0);
			i_100_ = i_100_ * i_95_ >> 8;
			i_101_ = i_101_ * i_95_ >> 8;
			int i_102_ = (i_91_ << 16) + (i_99_ * i_100_ + i_98_ * i_101_);
			int i_103_ = (i_97_ << 16) + (i_99_ * i_101_ - i_98_ * i_100_);
			int i_104_ = i_93_ + i * Renderer2D.width;
			for (i = 0; i < i_90_; i++) {
				int i_105_ = is_96_[i];
				int i_106_ = i_104_ + i_105_;
				int i_107_ = i_102_ + i_101_ * i_105_;
				int i_108_ = i_103_ - i_100_ * i_105_;
				for (i_93_ = -is[i]; i_93_ < 0; i_93_++) {
					Renderer2D.pixels[i_106_++] = (pixels[(i_107_ >> 16) + (i_108_ >> 16)
						* width]);
					i_107_ += i_101_;
					i_108_ -= i_100_;
				}
				i_102_ += i_100_;
				i_103_ += i_101_;
				i_104_ += Renderer2D.width;
			}
		} catch (Exception exception) {
			/* empty */
		}
	}

	public void method477(int i, int i_109_, int i_110_, int i_111_, int i_112_, int i_113_,
		int i_114_, double d, int i_115_) {
		if (i_113_ == -30658) {
			try {
				int i_116_ = -i_114_ / 2;
				int i_117_ = -i_112_ / 2;
				int i_118_ = (int) (Math.sin(d) * 65536.0);
				int i_119_ = (int) (Math.cos(d) * 65536.0);
				i_118_ = i_118_ * i >> 8;
				i_119_ = i_119_ * i >> 8;
				int i_120_ = (i_109_ << 16) + (i_117_ * i_118_ + i_116_ * i_119_);
				int i_121_ = (i_111_ << 16) + (i_117_ * i_119_ - i_116_ * i_118_);
				int i_122_ = i_110_ + i_115_ * Renderer2D.width;
				for (i_115_ = 0; i_115_ < i_112_; i_115_++) {
					int i_123_ = i_122_;
					int i_124_ = i_120_;
					int i_125_ = i_121_;
					for (i_110_ = -i_114_; i_110_ < 0; i_110_++) {
						int i_126_ = (pixels[(i_124_ >> 16) + (i_125_ >> 16) * width]);
						if (i_126_ != 0) {
							Renderer2D.pixels[i_123_++] = i_126_;
						} else {
							i_123_++;
						}
						i_124_ += i_119_;
						i_125_ -= i_118_;
					}
					i_120_ += i_118_;
					i_121_ += i_119_;
					i_122_ += Renderer2D.width;
				}
			} catch (Exception exception) {
				/* empty */
			}
		}
	}

	public void method478(IndexedSprite class50_sub1_sub1_sub3, int i, int i_128_) {
		i_128_ += offsetX;
		i += offsetY;
		int i_129_ = i_128_ + i * Renderer2D.width;
		int i_130_ = 0;
		int i_131_ = height;
		int i_132_ = width;
		int i_133_ = Renderer2D.width - i_132_;
		int i_134_ = 0;
		if (i < Renderer2D.topY) {
			int i_135_ = Renderer2D.topY - i;
			i_131_ -= i_135_;
			i = Renderer2D.topY;
			i_130_ += i_135_ * i_132_;
			i_129_ += i_135_ * Renderer2D.width;
		}
		if (i + i_131_ > Renderer2D.bottomY) {
			i_131_ -= i + i_131_ - Renderer2D.bottomY;
		}
		if (i_128_ < Renderer2D.topX) {
			int i_136_ = Renderer2D.topX - i_128_;
			i_132_ -= i_136_;
			i_128_ = Renderer2D.topX;
			i_130_ += i_136_;
			i_129_ += i_136_;
			i_134_ += i_136_;
			i_133_ += i_136_;
		}
		if (i_128_ + i_132_ > Renderer2D.bottomX) {
			int i_137_ = i_128_ + i_132_ - Renderer2D.bottomX;
			i_132_ -= i_137_;
			i_134_ += i_137_;
			i_133_ += i_137_;
		}
		if (i_132_ > 0 && i_131_ > 0) {
			method479(i_129_, i_133_, pixels, i_132_, Renderer2D.pixels,
				class50_sub1_sub1_sub3.pixels, i_131_, i_130_, 0, i_134_);
		}
	}

	private void method479(int i, int i_138_, int[] is, int i_139_, int[] is_140_, byte[] is_141_,
		int i_143_, int i_144_, int i_145_, int i_146_) {
		int i_147_ = -(i_139_ >> 2);
		i_139_ = -(i_139_ & 0x3);
		for (int i_148_ = -i_143_; i_148_ < 0; i_148_++) {
			for (int i_149_ = i_147_; i_149_ < 0; i_149_++) {
				i_145_ = is[i_144_++];
				if (i_145_ != 0 && is_141_[i] == 0) {
					is_140_[i++] = i_145_;
				} else {
					i++;
				}
				i_145_ = is[i_144_++];
				if (i_145_ != 0 && is_141_[i] == 0) {
					is_140_[i++] = i_145_;
				} else {
					i++;
				}
				i_145_ = is[i_144_++];
				if (i_145_ != 0 && is_141_[i] == 0) {
					is_140_[i++] = i_145_;
				} else {
					i++;
				}
				i_145_ = is[i_144_++];
				if (i_145_ != 0 && is_141_[i] == 0) {
					is_140_[i++] = i_145_;
				} else {
					i++;
				}
			}
			for (int i_150_ = i_139_; i_150_ < 0; i_150_++) {
				i_145_ = is[i_144_++];
				if (i_145_ != 0 && is_141_[i] == 0) {
					is_140_[i++] = i_145_;
				} else {
					i++;
				}
			}
			i += i_138_;
			i_144_ += i_146_;
		}
	}
}
